package 双亲委派模型;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class MyClassLoader extends ClassLoader {
    private String myName = "";

    public MyClassLoader(String myName) {
        this.myName = myName;
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        byte[] bytes = this.loadClassData(name);

        return this.defineClass(name, bytes, 0, bytes.length);

    }

    private byte[] loadClassData(String clsName) {
        byte[] data = null;
        InputStream in = null;
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        clsName = clsName.replace(".", "/");
        try {
            in = new FileInputStream(new File("classes/" + clsName + ".class"));

            int a = 0;
            while ((a = in.read()) != -1) {
                out.write(a);
            }
            return out.toByteArray();
        } catch (Exception e) {
            e.getStackTrace();
        }
        return data;
    }
}
